package es.us.lsi.daw.model.security;

import java.util.Calendar;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import javax.persistence.Transient;

import es.us.lsi.daw.annotations.Email;
import es.us.lsi.daw.annotations.Requerido;
import es.us.lsi.daw.model.util.BaseEntity;

@Entity
@Table(name = Tables.WEB_USER)
@PrimaryKeyJoinColumn(name=BaseEntity.A_ID)
public class WebUser extends User {

	private static final long serialVersionUID = -1383429783536592026L;

	public static final String	A_NAME				= "name";
	public static final String	A_SURNAME			= "surname";
	public static final String	A_PHONE				= "phone";
	public static final String	A_EMAIL				= "email";
	public static final String	A_DNI				= "dni";
	public static final String	A_CLD_REGISTER		= "cld_register";

	private static final String	C_NAME				= "name";
	private static final String	C_SURNAME			= "surname";
	private static final String	C_PHONE				= "phone";
	private static final String	C_EMAIL				= "email";
	private static final String	C_DNI				= "dni";
	private static final String	C_CLD_REGISTER		= "cld_register";
	
	@Column(name = C_CLD_REGISTER)
	private Calendar 			cldRegister;
	
	@Requerido
	@Column(name = C_NAME)
	private String				name;

	@Column(name = C_SURNAME)
	private String				surname;

	@Column(name = C_PHONE)
	private String				phone;
	
	@Column(name = C_DNI)
	private String				dni;

	@Requerido
	@Email
	@Column(name = C_EMAIL)
	private String				email;

	@Transient
	private String				errors;
	
	@Transient
	private String				repeatPassword;
	
	@Transient
	private boolean				passwordChange			= false;
	
	/**
	 * Copy constructor
	 * @param user
	 */
	public WebUser(WebUser user) {
		super(user);
		this.passwordChange=user.passwordChange;
		this.cldRegister=user.cldRegister;
		this.name=user.name;
		this.surname=user.surname;
		this.phone=user.phone;
		this.dni=user.dni;
		this.email=user.email;
		this.errors=user.errors;
		this.repeatPassword = user.repeatPassword;
		this.passwordChange = user.passwordChange;
	}
	
	public WebUser() {
		super();
	}

	public void resetPassword() {

		this.password = null;
		this.repeatPassword = null;
	}

	public boolean isPasswordChange() {

		return passwordChange;
	}

	public String getName() {
		return name;
	}

	public String getSurname() {
		return surname;
	}

	public String getPhone() {
		return phone;
	}

	public String getEmail() {
		return email;
	}

	public String getDni() {
		return dni;
	}
	
	public String getPassword() {

		return password;
	}

	public String getRepeatPassword() {
		return repeatPassword;
	}
	
	public void setName(String name) {
		this.name = name;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public void setDni(String dni) {
		this.dni = dni;
	}
	
	public void setPassword(String password) {

		if (this.password != null && password != null) {
			if (!this.password.equals(password)) {
				this.passwordChange = true;
			}
		} else if ((this.password == null && password != null) || (this.password != null && password == null)) {
			this.passwordChange = true;
		}
		this.password = password;
	}

	public Calendar getCldRegister() {
		return cldRegister;
	}

	public void setCldRegister(Calendar cldRegister) {
		this.cldRegister = cldRegister;
	}

	public String getErrors() {
		return errors;
	}

	public void setErrors(String errors) {
		this.errors = errors;
	}

	public void setRepeatPassword(String repeatPassword) {
		this.repeatPassword = repeatPassword;
	}

	public void setPasswordChange(boolean passwordChange) {
		this.passwordChange = passwordChange;
	}

}
